<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
<head>
  <link href="//gmpg.org/xfn/11" rel="profile">
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="generator" content="Hugo 0.69.0" />

  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>七牛云存储 &middot; 我的博客</title>

  
  <link type="text/css" rel="stylesheet" href="/myhugo/css/print.css" media="print">
  <link type="text/css" rel="stylesheet" href="/myhugo/css/poole.css">
  <link type="text/css" rel="stylesheet" href="/myhugo/css/syntax.css">
  <link type="text/css" rel="stylesheet" href="/myhugo/css/hyde.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Abril+Fatface|PT+Sans:400,400i,700">


  
  <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/apple-touch-icon-144-precomposed.png">
  <link rel="shortcut icon" href="/favicon.png">

  
  
</head>

  <body class="theme-base-0d ">
  <aside class="sidebar">
  <div class="container sidebar-sticky">
    <div class="sidebar-about">
      <a href="/myhugo/"><h1>我的博客</h1></a>
      <p class="lead">
       小白的逆袭之旅 
      </p>
    </div>

    <nav>
      <ul class="sidebar-nav">
        <li><a href="/myhugo/">Home</a> </li>
        
      </ul>
    </nav>

    <p>&copy; 2020. All rights reserved. </p>
  </div>
</aside>

    <main class="content container">
    <div class="post">
  <h1>七牛云存储</h1>
  <time datetime=2020-04-30T21:35:23&#43;0800 class="post-date">Thu, Apr 30, 2020</time>
  <p>###django接口获取token</p>
<pre><code>from qiniu import Auth

class Qiniu(APIView):
    def get(self,request):
        #声明认证对象 参数（ak,sk）
        q = Auth('lSHiV0YqwAZwm-benIiATiafSQ4-y63Zg1TLmXg3','KFZy2dDBwOEHIU2Qmb1v6Kx4C2zvILxkMNDpI057')
        #获取token
        token = q.upload_token('md-admin')
        return Response({'code':200,'token':token})

</code></pre><p>#vue</p>
<pre><code>&lt;template&gt;
  &lt;div&gt;
    &lt;table&gt;
        &lt;tr&gt;
            &lt;td&gt;
                七牛用户头像：
            &lt;/td&gt;
            &lt;td&gt;
                &lt;input type=&quot;file&quot; @change=&quot;qiniu&quot;&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
export default {
    data () {
        return {
            msg: &quot;这是一个变量&quot;,
            src:'',
            //七牛
            token:'',
        }
    },
    //钩子函数
    mounted:function(){
        //获取token
        this.get_token();
        },
    methods:{
        get_token:function(){
            this.axios.get('http://127.0.0.1:8000/qiniu/').then(res=&gt;{           
                    console.log(res)
                    this.token = res.data.token
                    console.log(this.token)
            })
        },
        //七牛上传
        qiniu:function(e){
            //获取文件
            let file = e.target.files[0];
            //声明表单
            let param = new FormData()    
            param.append('file',file,file.name)
            param.append('token',this.token)   
            //自定义zxios
            const axios_qiniu = this.axios.create({withCredentials:false})

            //发送请求
            axios_qiniu({
                method:'POST',
                url:'http://up-z1.qiniu.com',
                data:param,
                timeout:30000
            }).then(res=&gt;{
                console.log(res)
            })

        },
    }
}
&lt;/script&gt;</code></pre>
</div>


    </main>

    
  </body>
</html>
